home *** CD-ROM | disk | FTP | other *** search
/ Windows 6-Pak - Disc 5 / Windows 6-Pak (InfoMagic) (Disc 5) (1999).ISO / JAVA-Programming-Tools / apirc-1.1.exe / aprelay / apidentd.c next >
Encoding:
C/C++ Source or Header  |  1998-10-29  |  2.0 KB  |  98 lines

  1. /********************************************************/
  2. /*                  apidentd.c v1.0                     */
  3. /*             P.Toilon (zorthrax@id-net.fr)            */
  4. /*           29/10/1998 IDNET - Nancy - FRANCE          */
  5. /*     Apidentd is released into the Public Domain      */
  6. /*    The original file 'apidentd.c' has 2046 bytes     */
  7. /********************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <netdb.h>
  11. #include <signal.h>
  12. #include <sys/time.h>
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15.  
  16. int open_listener_socket(port,maxusers,timeout)
  17. int port,maxusers,timeout;
  18. {
  19.     time_t tvey;
  20.     struct sockaddr_in in;
  21.     int fd,rt,sz=sizeof(struct sockaddr_in);
  22.  
  23.     if((fd=socket(AF_INET,SOCK_STREAM,0))<0)
  24.         return(-1);
  25.  
  26.     memset(&in,0,sz);
  27.     in.sin_family=AF_INET;
  28.     in.sin_addr.s_addr=INADDR_ANY;
  29.     in.sin_port=htons(port);
  30.  
  31.     time(&tvey);
  32.  
  33.     while((rt=bind(fd,(struct sockaddr *)&in,sz))<0 && (time(0)-tvey)<timeout)
  34.         sleep(1);
  35.  
  36.     if(rt<0 || listen(fd,maxusers)<0)
  37.     {
  38.         close(fd);
  39.         return(-1);
  40.     }
  41.  
  42.     return(fd);
  43. }
  44.  
  45. /*
  46. ** The requester (e.g. irc server) asks :
  47. ** 6193, 23 : USERID : UNIX : Apircuser
  48. ** We may return :
  49. ** 6193 , 23 : USERID : UNIX : ap071
  50. */
  51.  
  52. main()
  53. {
  54.     struct sockaddr_in uin;
  55.     char *ptr1,*ptr2,frame[1024];
  56.     int cpt,nb,fd,mfd,osz,port1,port2;
  57.  
  58.     if((mfd=open_listener_socket(113,1,120))<0)
  59.     {
  60.         printf("\nSocket not openable. (Maybe another ident daemon yet running)\n");
  61.         exit();
  62.     }
  63.  
  64.     printf("\nApIdentd ready\n");
  65.     fflush(stdout);
  66.  
  67.     cpt=0;
  68.     osz=sizeof(struct sockaddr_in);
  69.  
  70.     /* If somebody comes, accept returns a positive value */
  71.     while(1)
  72.     {
  73.         if((fd=accept(mfd,(struct sockaddr *)&uin,&osz))>2)
  74.         {
  75.             if((nb=recv(fd,frame,1024,0))>5)
  76.             {
  77.                 ptr1=frame;
  78.                 ptr2=(char *)strchr(frame,',');
  79.  
  80.                 if(ptr2)
  81.                 {
  82.                     ptr2++;
  83.  
  84.                     while(*ptr1==' ') ptr1++;
  85.                     while(*ptr2==' ') ptr2++;
  86.  
  87.                     port1=atoi(ptr1);
  88.                     port2=atoi(ptr2);
  89.  
  90.                     sprintf(frame,"%d , %d : USERID : UNIX : ap%.3d\n",port1,port2,++cpt%1000);
  91.                     send(fd,frame,strlen(frame),0);
  92.                 }
  93.             }
  94.             close(fd);
  95.         }
  96.     }
  97. }
  98.